package com.lx.executor;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lx.entity.User;
import com.lx.response.Result;
import com.lx.response.ResultStatus;
import com.lx.service.UserService;
import com.lx.stereotype.Executor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerResponse;

import java.util.List;

import reactor.core.publisher.Mono;

import static org.springframework.web.reactive.function.server.ServerResponse.ok;

/**
 * 执行UserService的方法
 *
 * @author 段誉
 * @create 2019-05-15 15:07
 */
@Executor
public class UserExecutor {
  @Autowired
  private UserService userService;

  /**
   * 查询所有用户信息
   * @param
   * @return reactor.core.publisher.Mono<org.springframework.web.reactive.function.server.ServerResponse>
   * @author 段誉
   * @date 2019/5/15 15:13
   * @throws
   * @since 1.0.0
   */
  public Mono<ServerResponse> getAll() {
    return ok().contentType(MediaType.APPLICATION_JSON_UTF8)
            .body(BodyInserters.fromObject(userService.getAll()));
  }

  /**
   * 添加用户
   * @param user
   * @return reactor.core.publisher.Mono<org.springframework.web.reactive.function.server.ServerResponse>
   * @author 段誉
   * @date 2019/5/15 17:02
   * @throws
   * @since 1.0.0
   */
  public Mono<ServerResponse> add(User user) {
    userService.add(user);
    Result result = Result
            .builder()
            .status(ResultStatus.SUCCESS)
            .build();
    return ok().contentType(MediaType.APPLICATION_JSON_UTF8)
            .body(BodyInserters.fromObject(result));
  }

  /**
   * @desc 根据用户名进行模糊查询
   * @Author 段誉
   * @Date 2019/5/20 14:13
   * @method
   */
  public Mono<ServerResponse> getListByName(String name) {
    System.out.println("---------------------" + name);
    List<User> userList = userService.getListByName(name);
    return ok().contentType(MediaType.APPLICATION_JSON_UTF8)
            .body(BodyInserters.fromObject(userList));
  }

  /**
   * 分页查询信息
   * @param name
	 * @param pageNum
	 * @param pageSize
   * @return reactor.core.publisher.Mono<org.springframework.web.reactive.function.server.ServerResponse>
   * @author 段誉
   * @date 2019/5/20 15:19
   * @throws
   * @since 1.0.0
   */
  public Mono<ServerResponse> getListPageByName(String name, Integer pageNum, Integer pageSize) {
    System.out.println("name:" + name);
    System.out.println("pageNum:" + pageNum);
    System.out.println("pageSize:" + pageSize);
    Page<User> page = userService.getListPageByName(name, pageNum, pageSize);
    ObjectMapper objectMapper = new ObjectMapper();
    try {
      System.out.println(objectMapper.writeValueAsString(page));
    } catch (JsonProcessingException e) {
      e.printStackTrace();
    }
    return ok().contentType(MediaType.APPLICATION_JSON_UTF8)
            .body(BodyInserters.fromObject(page));
  }
}
